package com.itrh.base.service.impl;

import com.itrh.base.mapper.BaseMapper;
import com.itrh.base.query.BaseQuery;
import com.itrh.base.service.IBaseService;
import com.itrh.org.domain.Department;
import com.itrh.org.query.DepartmentQuery;
import com.itrh.org.util.Result;
import com.itrh.org.util.pageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public Result saveOrUpdate(T t) {
        return null;
    }

    @Override
    @Transactional
    public Result deleteById(Long id) {
        try {
            baseMapper.deleteById(id);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("操作失败");
        }
    }

    @Override
    public Result selectById(Long id) {
        try {
            T t = baseMapper.selectById(id);
            return Result.success(t);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("操作失败");
        }
    }

    @Override
    public Result selectAll() {
        try {
            List<T> list = baseMapper.selectAll();
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("操作失败");
        }
    }

    @Override
    public Result pageList(BaseQuery query) {
        // 分页查询
        // 查询总数
        Long count = baseMapper.count(query);
        // 查询数据
        List<T> list = baseMapper.pageList(query);
        return Result.success(new pageResult<>(count, list));
    }

    @Override
    @Transactional
    public Result batchRemove(List<Long> ids) {
        try {
            baseMapper.batchRemove(ids);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("操作失败");
        }
    }

}
